﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.ProfessionalTeam.DAL;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Text;
using Yipusoft.BasicBase.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;

public partial class MapInfo_ProfessionalTeam_VehicleNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.UrlReferrer != null)
            {
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }
            BindCodeItemList(VehicleClassList, "10");
            BindDdlVehicle();
            BindVehicleInfo();
            BindList();

        }
    }



    void BindList()
    {
        TeamDataContext tdc = new TeamDataContext();
        Guid pid = new Guid(Request["pid"]);
        List<ProsVehicles> list = tdc.ProsVehicles.Where(o => o.Pid == pid).ToList();
        gv.DataSource = list;
        gv.DataBind();
    }
    void BindDdlVehicle()
    {
        string where = " VehicleClass='" + VehicleClassList.SelectedValue + "' and DepartmentId='" + Request["DepartmentId"] + "' ";
        List<VehicleInfo> list = Vehicle.GetList(where);
        ddlVehicle.DataSource = list;
        ddlVehicle.DataBind();
    }
    void BindVehicleInfo()
    {
        if (ddlVehicle.SelectedValue != "")
        {
            Guid Id = new Guid(ddlVehicle.SelectedValue);
            VehicleInfo info = Vehicle.GetDetail(Id);

            lblDetail.Text = "<a href=javascript:OpenWin('../../BasicBase/Vehicle/VehicleView.aspx?ID=" + Id + "');>查看</a>";
        }
        else
        {
            lblDetail.Text = "";
        }
    }
    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList2(" GroupID='XFZBLB' and ParentID='" + parentID + "' and ItemID<>'1099' order by SeqID,ItemID ");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }

    protected void VehicleClassList_SelectedIndexChanged(object sender, EventArgs e)
    {

        BindDdlVehicle();
        BindVehicleInfo();
    }

    public string GetCodeItem(string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass);
        return info != null ? info.ItemName : "";
    }


    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            #region 赋值
            Guid VehicleId = new Guid(gv.DataKeys[e.Row.RowIndex][0].ToString());
            VehicleInfo vehicle = Vehicle.GetDetail(VehicleId);
            Label DeviceName = e.Row.FindControl("DeviceName") as Label;
            DeviceName.Text = vehicle.VehicleName;
            Label DeviceClass = e.Row.FindControl("DeviceClass") as Label;

            CodeItemInfo item = CodeItem.GetCodeItem(vehicle.VehicleClass, "XFZBLB");
            if (item != null)
            {
                DeviceClass.Text = item.ItemName;
            }
            else
            {
                DeviceClass.Text = "--";
            }


            #endregion
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Pid = Request["Pid"];
        string DepartmentId = Request["DepartmentId"];
        TeamDataContext tdc = new TeamDataContext();
        ProsVehicles vehicle = new ProsVehicles();
        vehicle.Id = Guid.NewGuid();
        vehicle.LastDate = DateTime.Now;
        vehicle.Pid = new Guid(Pid);
        vehicle.SetUserId = User.Identity.Name;
        vehicle.VehicleId = new Guid(ddlVehicle.SelectedValue);
        tdc.ProsVehicles.InsertOnSubmit(vehicle);
        tdc.SubmitChanges();
        BindList();
    }

    protected void ddlDevice_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindList();
        BindVehicleInfo();
    }




    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
  
        TeamDataContext tdc = new TeamDataContext();

        if (e.CommandName == "Del")
        {
            Guid id = new Guid(e.CommandArgument.ToString());

            ProsVehicles vehicles = tdc.ProsVehicles.Where(o => o.Id == new Guid(e.CommandArgument.ToString())).FirstOrDefault();
            tdc.ProsVehicles.DeleteOnSubmit(vehicles);
            tdc.SubmitChanges();
            BindList();
        }
    }

    protected void lblReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"].ToString());
    }
}

